const { Router } = require('express')
const express = require('express')
//路由容器
const center = express.Router()
const axios = require('axios')
// center.get('/center', (req, res) =>{
//     res.render('home/center')
// })
const multipart = require('connect-multiparty');
const FormData = require('form-data')
var multipartMiddleware = multipart()
const fs = require("fs");
const { JsonWebTokenError } = require('jsonwebtoken')
const jwt = require("jsonwebtoken")

center.get('/user/updateInformation', (req, res) => {
    let {userId,telephone,nickname,sex,birth}=req.query
    axios({
        method:'put',
        url:'http://www.mianded.top:8081/user/updateInformation',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            token:req.session.token
        },
        params:{
            userId:jwt.decode(req.session.token).id,
            telephone:telephone,
            nickname:nickname,
            sex:sex,
            birth:birth
        }
    }).then(function(data){
        res.status(200).json({
            data:data.data,
            code:0
        })
    }).catch(function(err){
        res.status(500).json({
            msg:'失败'
        })
        console.log(err);
    })
 })
// center.put('/user/uploadHeadImg', (req, res) => {
//     console.log(req.body.params)
//     let {img,userId}=req.body.params
//     axios({
//         method:'put',
//         url : '/user/uploadHeadImg',
//         params:{
//             img:img,
//             userId:userId
//         }
//     }).then(function(data){
//         res.status(200).json({
//             data:data.data,
//             code:0
//         })
//     }).catch(function(err){
//         res.status(500).json({
//             msg:err
//         })
//     })
//  })




center.put('/user/uploadHeadImg',multipartMiddleware, (req, res) => {
    // console.log(req.body.params)
    // let {userId}=req.body.params
        let myfile = req.files.myfile;
        console.log("_---------------------_")
        console.log(myfile)
        console.log("_---------------------_")
        let file = req.files.multipartFile;
        let userId = req.query.userId;
        console.log(userId);
        console.log(file.headers)
        const uri = file.path
        console.log("-----------------------------")
        console.log(uri);
        console.log("-----------------------------")
        let multipartFile = new FormData();
        multipartFile.append('img', fs.createReadStream(uri));
        axios.put(`user/uploadHeadImg?userId=${userId}`, multipartFile, { headers: multipartFile.getHeaders() }).then((data) => {
            console.log("success============")
            console.log(data.data)
            res.send({ data: data.data })
        }).catch((err) => {
            console.log("err");
            res.send(err)
        })
})

module.exports=center;